package com.xuhuan.demo.date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author huan.xu
 * @Date 2023/5/31 15:21
 * @Description 日期工具类
 */
public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private static String FORMAT_TEN = "yyyy-MM-dd";

    public static void main(String[] args) throws Exception {
        String date1="2023-02-21";
        printMonthWeek(formatDate(date1,FORMAT_TEN));
        System.out.println("---------------------------------------------------------");
        String date2="2023-05-21";
        printMonthWeek(formatDate(date2,FORMAT_TEN));
        System.out.println("---------------------------------------------------------");
        String date3="2023-06-21";
        printMonthWeek(formatDate(date3,FORMAT_TEN));
    }

    /**
     * 打印每个月每周的开始日期、结束日期
     * @param date
     */
    public static void printMonthWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        logger.info("本月是：{}", formatDate(date,FORMAT_TEN));
        //获取本月总周数
        int weeks = c.getActualMaximum(Calendar.WEEK_OF_MONTH);
        int startMonthDay = c.getActualMinimum(Calendar.DAY_OF_MONTH);
        int endMonthDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);

        int startWeekDay = startMonthDay;
        int endWeekDay = 0;
        for (int i = 1; i <= weeks; i++) {
            Calendar startWeek = Calendar.getInstance();
            startWeek.set(Calendar.YEAR, c.get(Calendar.YEAR));
            startWeek.set(Calendar.MONTH, c.get(Calendar.MONTH));
            startWeek.set(Calendar.DAY_OF_MONTH, startWeekDay);

            // 星期几
            if (i == 1) {
                int dayWeek = startWeek.get(Calendar.DAY_OF_WEEK) - 1;
                endWeekDay = 7 - dayWeek + 1;
                startWeekDay = startWeekDay + endWeekDay;
            } else {
                startWeekDay = startWeekDay + 7;
            }

            Calendar endWeek = Calendar.getInstance();
            endWeek.set(Calendar.YEAR, c.get(Calendar.YEAR));
            endWeek.set(Calendar.MONTH, c.get(Calendar.MONTH));
            if (i != weeks) {
                endWeek.set(Calendar.DAY_OF_MONTH, endWeekDay);
            } else {
                endWeek.set(Calendar.DAY_OF_MONTH, endMonthDay);
            }
            logger.info("本月第{}周：开始日期[{}]，结束日期[{}]", i, formatDate(startWeek, FORMAT_TEN), formatDate(endWeek, FORMAT_TEN));

            endWeekDay = endWeekDay + 7;
        }
    }

    /**
     * 格式化日期字符串
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static Date formatDate(String date, String format) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.parse(date);
    }

    /**
     * 格式化日期
     *
     * @param calendar
     * @param format
     * @return
     */
    public static String formatDate(Calendar calendar, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }
}
